<?php

namespace app\api\controller;

use think\facade\Db;
use app\common\model\Comment as C;

class Comment extends ApiBase
{
    // 不需要验证token的方法
    protected $middleware = [
        'app\api\middleware\Auth' => ['except' => ["list"]],
    ];

    //评论列表
    public function list()
    {
        $postId = input("post_id");
        $where["pid"] = 0;
        $uid = $this->uid;
        if (!empty($postId)) {
            $where["post_id"] = $postId;
        }

        $list = C::withJoin(["userInfo"])->order("id desc")->where($where)->paginate(10)->each(function($item) use ($uid) {

            $item["thumbs"] = Db::name("comment_thumbs")->where("c_id",$item["id"])->count();

            $item["is_thumbs"] = false;
            $isCon = Db::name("comment_thumbs")->where(["uid" =>$uid,"c_id" => $item["id"]])->find();
            if($isCon){
                $item["is_thumbs"] = true;
            }

            $children = C::withJoin(["userInfo"])->where("pid",$item["id"])->select()->each(function ($item2) use ($uid){
                $item2["to_user"] = Db::name("user")->where("uid",$item2["to_uid"])->find();
                $item2["thumbs"] = Db::name("comment_thumbs")->where("c_id",$item2["id"])->count();

                $item2["is_thumbs"] = false;
                $isCon = Db::name("comment_thumbs")->where(["uid" =>$uid,"c_id" => $item2["id"]])->find();
                if($isCon){
                    $item2["is_thumbs"] = true;
                }

            });

            if(count($children) > 0){
                $item["children"] = $children;

            }else{
                $item["children"] = [];
            }

            return $item;

        });

        return success($list);
    }

    //评论点赞
    public function thumbs(){
        $id = input("id");

        $data["c_id"] = $id;
        $data["uid"] = $this->uid;
        $isCon = Db::name("comment_thumbs")->where($data)->find();
        if($isCon){
            return error("请勿重复点赞");
        }

        $data["create_time"] = time();

        $res = Db::name("comment_thumbs")->insert($data);

        if($res){
            $comment = C::find($id);
            $this->sendMsg($this->uid, $comment["uid"], $comment["post_id"], 1, "赞了您的评论：" . $comment["content"]);
            return notice("点赞成功");
        }

        return error("点赞失败");
    }

    //取消点赞
    public function cancelThumbs(){
        $id = input("id");

        $where["c_id"] = $id;
        $where["uid"] = $this->uid;

        $res = Db::name("comment_thumbs")->where($where)->delete();
        if($res){
            return notice("已取消点赞");
        }

        return error("取消失败");
    }

    //删除评论
    public function del(){
        $id = input("id");

        $commentInfo = C::where("id",$id)->find();
        $res = C::destroy($id);
        if ($res) {
            //自己删除评论时不发送消息
            if($this->uid != $commentInfo["uid"]){
                $this->sendMsg(2,$commentInfo["uid"],0,6,"您的评论疑似违反规定，已被删除：'".$commentInfo['content']."'");
            }
            return notice("删除成功");
        }
        return error("删除失败");
    }
}
